<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @filename agent.php
 * @desc    代理商操作
 * @author  homingway@gmail.com
 * @update  2010-01-05
 */
class Agent extends MY_Controller {

	function Agent(){
		parent::MY_Controller();
        $this->load->model('Magent');
        $this->load->helper('page');
        page_common();
	}

    function index() {
        self::login();
    }

    /**
     * 地区代理商登录
     * 这里请使用用户名登录，email和真实姓名不可登录
     */
    function login() {
        if($this->input->post('action') == 'login'){
            //此处省略验证码的功能
            $agent_no = $this->input->post('agent_no');
            $agent_pwd = $this->input->post('agent_pwd');
            $flag = $this->Magent->check_agent_user($agent_no,$agent_pwd);
            if($flag > 0){
                //登录成功，设置session
                $info = $this->Magent->get_agent_info($flag);
                self::set_agent_session($info);
                $data = array();
                $data['last_time'] = date('Y-m-d H:i:s');
                $data['login_times'] = $info['login_times']+1;
                $data['last_ip'] = $this->input->ip_address();
                $this->Magent->update_agent($info['agent_id'],$data);
                parent::redirect('欢迎你：'.$info['agent_name'],base_url().'agent/orders/');
            } elseif ($flag == '-2'){
                //用户不存在
                parent::redirect('用户名：'.$agent_no.' 不存在','back');
            } elseif ($flag == '-3'){
                //密码错误
                parent::redirect('密码错误，请返回重新填写！','back');
            }
            die();
        }
        $this->template->view('v1/agent_login');
    }

    /**
     * 代理商订单列表
     */
    function orders($offset=0){
        $this->check_agent_login();

        self::agent_left();

        $agent = self::get_agent_session();
        $agent_no = $agent['agent_no'];

        $percent = get_config_value('agent_discount');
        
        $date = getdate();
        $year = $date['year'];
        $month = $date['mon'];

        $total = $this->Magent->get_nb_orders_month_count($agent_no,$year,$month);

        $month_count = $this->Magent->get_agent_orders_month_count($agent_no,$year,$month);
        $month_price = $this->Magent->get_agent_month_price($agent_no,$year,$month);
        $month_discount = round($month_price*$percent/100,2);
        $nb_count = $this->Magent->get_agent_nb_count($agent_no);

        $summory = array('month_count'=>$month_count,'month_price'=>$month_price,'month_discount'=>$month_discount,'nb_count'=>$nb_count,'total'=>$total);

		$this->load->library('pagination');

		$config['base_url'] = '/agent/orders/';
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;
		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$limit = $config['per_page'];

        $temp = $this->Magent->get_nb_orders_month($agent_no,$year,$month,$offset,$limit);

        $orders = array();
        if(!empty($temp)){
            foreach($temp as $tmp){
                $tmp['discount'] = round(($tmp['order_price']*$percent/100),2);
                $orders[] = $tmp;
            }
        }

        $this->template->view('v1/agent_orders',array('orders'=>$orders,'page_html'=>$page_html,'year'=>$year,'month'=>$month,'summory'=>$summory));
        
    }

    /**
     * 代理商订单历史记录
     */
    function history($offset=0){
        $this->check_agent_login();
        self::agent_left();

        $agent = self::get_agent_session();
        $agent_no = $agent['agent_no'];

        $percent = get_config_value('agent_discount');

        $date = getdate();
        $year = $date['year'];
        $month = $date['mon'];

        $total = $this->Magent->get_agent_history_order_count($agent_no,$year,$month);
        
        $this->load->library('pagination');

		$config['base_url'] = '/agent/history/';
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;
		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$limit = $config['per_page'];

        $orders = $this->Magent->get_agent_history_orders($agent_no,$year,$month,$offset,$limit);

        $this->template->view('v1/agent_history',array('orders'=>$orders,'page_html'=>$page_html,'year'=>$year,'month'=>$month));
    }

    /**
     * 代理商所属某个网吧推广员的订单信息
     */
    function nborders($nb='',$offset=0){
        $this->check_agent_login();
        if($nb == ''){
            parent::message('选中网吧推广员后再继续！');
        }
        self::agent_left();
        $date = getdate();
        $year = $date['year'];
        $month = $date['mon'];

        $this->load->model('Morders');

        $total = $this->Morders->get_nb_order_count($nb,$year,$month);

        $this->load->library('pagination');

		$config['base_url'] = '/agent/nborders/'.$nb.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;
		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$limit = $config['per_page'];

        $orders = $this->Morders->get_nb_orders($nb,$year,$month,$offset,$limit);

        $this->template->view('v1/agent_nborders',array('orders'=>$orders,'page_html'=>$page_html,'year'=>$year,'month'=>$month,'nb'=>$nb));
    }

    /**
     * 代理商所属某个网吧推广员的详细信息
     */
    function nbdetail($nb=''){
        $this->check_agent_login();
        if($nb == ''){
            parent::message('选中网吧推广员后再继续！');
        }
        self::agent_left();
        $this->load->model('Mnetbar');
        $info = $this->Mnetbar->get_barinfo_uid($nb);
        $this->template->view('v1/agent_nbdetail',array('info'=>$info));
    }

    function agent_left(){
        $agent = self::get_agent_session();
        $agent_no = $agent['agent_no'];
        $date = getdate();
        $year = $date['year'];
        $month = $date['mon'];
        $info = $this->Magent->get_agent_info_by_no($agent_no);
        $order_count = $this->Magent->get_agent_history_order_count($agent_no,$year,$month);
        $discount = $this->Magent->get_agent_history_dicount($agent_no,$year,$month);
        $this->template->assign_array(array('agent_discount'=>$discount,'agent_order_count'=>$order_count,'agent'=>$info));
    }

    //检测代理商是否已经登录
    function check_agent_login() {
        $this->load->library('session');
		$session = $this->session->userdata('agent');
        if(!$session){ //SESSION不存在，直接跳转到登录页面
            redirect(base_url().'agent/login/');
        }
    }

    function get_agent_session(){
        return parent::get_session_value('agent');
    }

    /**
     * 代理商登录后设置其session信息
     * @param <array> $info
     */
    function set_agent_session($info) {
        $arr = array('agent_id'=>$info['agent_id'],'agent_no'=>$info['agent_no'],'agent_name'=>$info['agent_name']);
        parent::set_session_value('agent',$arr);
    }

    function logout(){
        parent::unset_session_value('agent');
        parent::redirect('注销成功！',base_url().'agent/login/');
    }

}

?>